package com.herench.bunch.clinic.support;

import com.herench.bunch.commons.utils.DataFormatUtils;
import com.herench.bunch.commons.utils.IdGenerator;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

public class ProxyMsgTemplate {

    public static Document getOutMsgTpl(String responseXml) throws Exception {
//        Document document = DocumentHelper.parseText(responseXml);
        Document document = DocumentHelper.parseText(responseXml);
        return document;
    }
    public static String getStackTrace(Throwable throwable)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        try
        {
            throwable.printStackTrace(pw);
            return sw.toString();
        } finally
        {
            pw.close();
        }
    }



    public static Document getInMsgTpl(String tradeCode) {
        Document document = DocumentHelper.createDocument();
        Element requestElement = document.addElement("Request");
        requestElement.addElement("TradeCode").addText(tradeCode);
        requestElement.addElement("TerminalCode").addText("YK10102012");
        requestElement.addElement("MachineIP").addText("0.0.0.0");
        requestElement.addElement("TransNO").addText(IdGenerator.getUUID());
        return document;
    }

    public static Document getInMsgInHos(String tradeCode) {
        Document document = DocumentHelper.createDocument();
        Element requestElement = document.addElement("Request");
        requestElement.addElement("TradeCode").addText(tradeCode);
        requestElement.addElement("TerminalCode").addText("YK10102012");
        requestElement.addElement("MachineIP").addText("0.0.0.0");
        requestElement.addElement("TransNO").addText(IdGenerator.getUUID());
        requestElement.addElement("OpName").addText("住院自助00");
        requestElement.addElement("Source").addText("3");
        requestElement.addElement("PatientID").addText("00000000-0000-0000-0000-000000000000");
        return document;
    }

    public static Document getInMsgAppoint(String method) {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("BITCRoot");
        root.addElement("CommitData");
        root.addElement("ReturnData");
        Element operaInfoElement = root.addElement("OperateInfo").addElement("Info");
        operaInfoElement.addAttribute("METHOD", method).addAttribute("OPT_ID", "10000003").addAttribute("OPT_NAME", "诊间预约").addAttribute("OPT_IP", "172.16.231.22").addAttribute("OPT_DATE", DataFormatUtils.getSimpleTimeString(new Date())).addAttribute("GUID", IdGenerator.getUUID()).addAttribute("TOKEN", IdGenerator.getUUID()).addAttribute("OPT_INSIDE_ID", "1107");
        Element resultInfoElement = root.addElement("Result").addElement("Info");
        resultInfoElement.addAttribute("EXECUTE_FLAG", "").addAttribute("EXECUTE_MESSAGE", "").addAttribute("ACCOUNT","");
        return document;
    }

}
